package com.panghu.controller;


import com.panghu.entity.Admin;
import com.panghu.entity.ApiResult;
import com.panghu.service.AdminService;
import com.panghu.utils.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 管理员信息表 前端控制器
 * </p>
 *
 * @author huahua
 * @since 2022-03-19
 */
@RestController

public class AdminController {

    @Autowired
    private AdminService adminService;

    //关于admin的增删改查
    @GetMapping("/admins")
    public ApiResult findAll(){
        System.out.println("hahha");
        //查询所有的管理员
        List<Admin> list = adminService.list();
        System.out.println("已经收到请求");
        System.out.println(list);
        return ApiResultHandler.success(list);
    }

    @GetMapping("/admin/{adminId}")
    public ApiResult findById(@PathVariable("adminId") Integer adminId){
        //按id查询管理员
        return ApiResultHandler.success(adminService.getById(adminId));
    }

    @DeleteMapping("/admin/{adminId}")
    public ApiResult deleteById(@PathVariable("adminId") Integer adminId){
        //按id删除管理员
        adminService.removeById(adminId);
        return ApiResultHandler.success();
    }

    @PutMapping("/admin/{adminId}")
    public ApiResult update(@PathVariable("adminId") Integer adminId, Admin admin){
        return ApiResultHandler.success(adminService.updateById(admin));
    }

    @PostMapping("/admin")
    public ApiResult add(Admin admin){
        //增加管理员
        return ApiResultHandler.success(adminService.save(admin));
    }
}

